package git.jas0nch.final4it.controller;

import com.alibaba.fastjson.JSONObject;
import git.jas0nch.final4it.service.IMLService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * projectName : final4it
 * className: MLController
 * description: 可视化数据的请求响应。
 *
 * @author : chengjiang@jasonch.tech
 * @date: 2022/05/03
 */

@Controller
@RequestMapping("/ML/v1/")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class MLController {

    private final IMLService imlService;

    private final RestTemplate restTemplate;

//    @PostMapping("/file")
//    public void getFile(MultipartFile file){
//
//    }

    @GetMapping("/training/{dataName}")
    public String getPage(@PathVariable String dataName, Model view){
        // 这里借用微服务的远程调用思想，调取本地请求获取本地数据。
        // 测试使用，所以并无考虑其耦合程度。
        // localhost: 127.0.0.1
        Object result = restTemplate.getForObject("http://127.0.0.1:10086/ML/v1/train/" + dataName, String.class);
        log.info("the data result:{}", result);
        view.addAttribute("result", result);
        return "keshihua";
    }

    @GetMapping("/train/{dataName}")
    @ResponseBody
    public JSONObject executePY(@PathVariable String dataName){

        return imlService.executePY(dataName);
    }
}
